﻿/*****************************************介绍*****************************************
 * 作者：User
 * 创建时间：2025-09-08 18:48:14
 * 功能：画线示例
**************************************************************************************/

using Game.Framework;
using Game.UI;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class LineExample : MonoBehaviour, ILineObj
{
    public UILineRenderer lineRenderer;
    public GameObject linePrefab;

    private void Awake()
    {
        PoolManager.Instance.CreatePool("LineTest", linePrefab, 10);
    }

    private void Start()
    {
        lineRenderer.Init(this);

        // 示例点列表（在UI空间中的坐标）
        List<Vector2> points = new List<Vector2>
        {
            new Vector2(0, 0),
            new Vector2(0, 50),
            new Vector2(200, 50),
            new Vector2(200, 100),
            new Vector2(400, 100)
        };

        // 绘制带动画的线条
        //lineRenderer.DrawLine(points, true);

        // 如果要绘制不带动画的线条，可以这样调用：
        lineRenderer.DrawLine(points, true);
    }

    // 示例：动态更新线条
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            // 创建随机点
            List<Vector2> randomPoints = new List<Vector2>();
            Vector2 startPoint = new Vector2(0, 0);
            randomPoints.Add(startPoint);

            for (int i = 1; i < 5; i++)
            {
                Vector2 point = new Vector2(i * 100, Random.Range(-100, 100));
                randomPoints.Add(point);
            }

            // 重新绘制线条
            lineRenderer.DrawLine(randomPoints, true);
        }

        if (Input.GetKeyDown(KeyCode.C))
        {
            // 清除线条
            lineRenderer.ClearLine();
        }
    }

    public Image Get()
    {
        var obj = PoolManager.Instance.Get("LineTest");
        return obj.GetComponent<Image>();
    }

    public void Delete(GameObject line)
    {
        PoolManager.Instance.Delete("LineTest", line);
    }

    private void OnDestroy()
    {
        PoolManager.Instance.ClearPool("LineTest");
    }
}